package auth

import (
	// "github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"net/http"
	"server/lib/md5"
	"server/web/model/shop/admin/auth"
	"strings"
	// "server/conf"
	"server/lib/Imartini"
)

func Register(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog) {
	ret := map[string]interface{}{}

	ret["msg"] = req.FormValue("error")
	r.HTML("shop/admin/register.html", ret)
}

func PRegister(res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog) {
	username := req.PostFormValue("username")
	password := req.PostFormValue("password")
	password2 := req.PostFormValue("password2")
	tel := strings.Replace(req.PostFormValue("tel"), " ", "", -1)

	if password != password2 {
		http.Redirect(res, req, "/admin/register?error=两次输入的密码不正确", http.StatusFound)
		return
	} else if username == "" || password == "" || tel == "" {
		http.Redirect(res, req, "/admin/register?error=请输入完整的注册信息", http.StatusFound)
		return
	}

	if 1 == auth.Isexist(username) {
		http.Redirect(res, req, "/admin/register?error=用户名已存在", http.StatusFound)
		return
	}

	param := map[string]interface{}{
		"username": username,
		"passwd":   md5.Md5(password),
		"tel":      tel,
	}
	auth.Register(param)
	http.Redirect(res, req, "/admin/login?msg=注册成功，请登陆", http.StatusFound)
}
